<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\common\controller\Frontend;
use think\facade\Db;
use think\facade\View;

class Ucenter extends Frontend
{
    public function initialize()
    {
        parent::initialize();
        global $userinfo;
        $userinfo = $this->visitor->info;
        View::assign('uid', $userinfo['id']);
        $user = Db::name('user')->where(array('id' => $userinfo['id']))->find();
        $userroleid = $user['roleid'];
        $isadmin = getisadmin($userroleid);
        View::assign('isadmin', $isadmin);
        $action = request()->action();
        $action_name = request()->action();
        if (($action_name == 'setavatar') or ($action_name == 'setmail') or ($action_name == 'setpassword')) {
            $actionname = 'index';
        } elseif ($action_name == 'guanzhu') {
            $actionname = 'fensi';
        } elseif ($action_name == 'mailmess') {
            $actionname = 'usermail';
        } else {
            $actionname = $action_name;
        }
        View::assign('actionname', $actionname);
        $hottag = Db::name('tag')->order('count desc')->select();
        View::assign('hottag', $hottag); //热门标签
        $tjdoc = tjdoc();
        View::assign('tjdoc', $tjdoc); //编辑推荐文档
        View::assign('action', $action);
    }



    public function index() {
        global $userinfo;
        if(!$userinfo)  return redirect('/home/user/login');
        $uid = $userinfo['id'];
        $mod = Db::name('userinfo');
        if (request()->isPost()) {
            return fail(0,'暂未开发完');
        } else {
            $info = $mod->where(array('uid' => $uid))->find();
            //dump($info);
            // 加入积分文档等数据统计 - wf添加
            $info2 = Db::name('user_scoresum')->where(array('uid' => $uid))->find();
            $info2['system'] = $info2['login'] + $info2['register'];
            View::assign('info2', $info2);
            View::assign('info', $info);
            return view();
        }
    }

    public function mydoclist() {
        global $userinfo;
        if(!$userinfo)  return redirect('/home/user/login');
        $uid = $userinfo['id'];
        var_dump($uid);
        $type = request()->param('type')?request()->param('type'):0;
        $tag =[
            'type'=>"lists",
            'uid'=>$uid,
            'status_type'=>$type,
            'own'=>1,
            'order'=>"create_time desc",
            'pagenum'=>15,

        ];
        $data = getDocLists($tag);
        View::assign('data', $data);
        View::assign('uid', $uid);
        View::assign('type', $type);

        return view();
    }


    public function xhdoclist() {
        global $userinfo;
        if(!$userinfo)  return redirect('/home/user/login');
        $uid = $userinfo['id'];
        View::assign('uid', $uid);
        return view();
    }

    public function downdoclist() {
        global $userinfo;
        if(!$userinfo)  return redirect('/home/user/login');
        return view();
    }

    public function usermail() {
        global $userinfo;
        if(!$userinfo)  return redirect('/home/user/login');
        $uid = $userinfo['id'];
        $mod = Db::name('user_mail');
        $map = '(pb=0 AND re_id=0) OR (pb=1 AND re_id=0 AND fromid=' . $userinfo['id'] . ')';
        //$map['re_id']=0;//回复的信息不单独显示
        $data = $this->_list( $mod, $map);
        $map1['new'] = 1;
        $map1['toid'] = $userinfo['id'];
        $idlist = $mod->where($map1)->field('re_id')->select();
        foreach ($data['list'] as $key1 => $value1) {
            foreach ($idlist as $key => $value) {
                if ($value == $value1['id']) {
                    $data['list'][$key]['renew'] = 1;
                }
            }
        }
        View::assign('data', $data);
        View::assign('uid', $userinfo['id']);
        return view();
    }


    public function tongji() {
        global $userinfo;
        if(!$userinfo)  return redirect('/home/user/login');
        $uid = $userinfo['id'];
        $info = Db::name('user_scoresum')->where(array('uid' => $uid))->find();
        $info['system'] = $info['login'] + $info['register'];
        View::assign('info', $info);
        $payment = Db::name('Payment');
        $paymentmap['status'] = 1;
        $paymentdata = $payment->where($paymentmap)->select();
        View::assign('paymentdata', $paymentdata);
        return view();
    }

    public function mail_del() { //删除消息
        global $userinfo;
        $mod = Db::name('user_mail');
        //删除消息的同时 根据itemid删除屏蔽数据库中的数据
        $id = trim(request()->param('id'));
        if (false !== $mod->where('id',$id)->delete()) {
            return success(1,'删除成功');
        } else {
            return fail(0,'删除失败');
        }

    }

}